package threadlocal;

import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

public class ThreadLocalTest {
    @Test
    public void testThreadLocal() throws Exception {
        Bank bank = new Bank();
        Transfer transfer = new Transfer(bank);
        Transfer2 transfer2 = new Transfer2(bank);
        Thread thread1 = new Thread(transfer);
        thread1.start();
        Thread thread2 = new Thread(transfer2);
        thread2.start();
        thread1.join();
        thread2.join();
        System.out.println(bank.getInteger());
        println(Thread.currentThread().getName());
    }

    @Test
    public void testStream() {
        List<String> strs = Arrays.asList("d", "b", "a", "c", "a");
        Optional<String> min = strs.stream().min(Comparator.comparing(t -> t));
        Optional<String> max = strs.stream().max(String::compareTo);
        System.out.println(String.format("min:%s; max:%s", min.get(), max.get()));// min:a; max:d

        Optional<String> aa = strs.stream().filter(str -> !str.equals("a")).findFirst();
        Optional<String> bb = strs.stream().filter(str -> !str.equals("a")).findAny();

        Optional<String> aa1 = strs.parallelStream().filter(str -> !str.equals("a")).findFirst();
        Optional<String> bb1 = strs.parallelStream().filter(str -> !str.equals("a")).findAny();

        System.out.println(aa.get() + "===" + bb.get());// d===d
        System.out.println(aa1.get() + "===" + bb1.get());// d===b or d===c
        Function<String, String> function = this.identity();
        println(function.apply("nishizhume"));
    }

    <T> Function<T, T> identity() {
        return t -> t;
    }

    private void println(Object object) {
        System.out.println(object);
    }
}
